﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CSP.Model.Presenter;
using CSP.Model.Entities;
using System.Collections.Generic;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;

namespace eGov.SharePoint.MyPeeps.WebControls
{
    public partial class MyPeepsUpdates : System.Web.UI.UserControl, IMyPeepsUpdatesPresenter
    {
        private MyPeepsUpdatesPresenter presenter = null;
        protected global::System.Web.UI.WebControls.Repeater rptMyPeepsUpdates;
        protected global::System.Web.UI.WebControls.HyperLink lnkViewAllActivity;
        protected global::System.Web.UI.WebControls.Literal literal1;


        public int DisplayedRows { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                presenter = new MyPeepsUpdatesPresenter(this);

                //if (DisplayedRows < 15)
                //{
                //    SetViewAllActivityUrl();
                //}
                //else
                //{
                //    lnkViewAllActivity.Visible = false;
                //}

                presenter.Load();
            }
            catch (Exception ex)
            {
                DisplayError(ex);
            }
        }

        private void SetViewAllActivityUrl()
        {
            string currentLoggedUser = string.Empty;

            SPWeb site = SPContext.Current.Web;
            SPUser currentUser = site.CurrentUser;
            //SPSite siteCollection = SPContext.Current.Site;
            //SPWebApplication webApp = SPContext.Current.Site.WebApplication;
            //SPSiteCollection siteCollection = webApp.Sites;


            if (currentUser != null)
            {
                currentLoggedUser = currentUser.LoginName;
                if (currentLoggedUser.Contains('\\'))
                    currentLoggedUser = currentLoggedUser.Split('\\')[1];
            }

            UserProfileManager profileManager = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site));

            string allActivityUrl = string.Format("/" + profileManager.PersonalSiteInclusion + "/{0}/ViewAllActivity.aspx", currentLoggedUser.Replace(".", "_"));
            lnkViewAllActivity.NavigateUrl = string.Format(SPContext.Current.Site.WebApplication.Sites["/"].Url + allActivityUrl);

        }

        public void BindMyPeepsUpdates(List<Update> colleagueUpdates)
        {
            try
            {
                SetViewAllActivityUrl();  
                //colleagueUpdates.Sort(delegate(Update U1, Update U2) { return U1.EventTime.CompareTo(U2.EventTime); });

                if (colleagueUpdates.Count > 0)
                {
                    rptMyPeepsUpdates.DataSource = colleagueUpdates.GetRange(0, colleagueUpdates.Count >= DisplayedRows ? DisplayedRows : colleagueUpdates.Count);
                    rptMyPeepsUpdates.DataBind();

                }

                if (rptMyPeepsUpdates.Items == null || rptMyPeepsUpdates.Items.Count < 1)
                {
                    literal1.Visible = true;
                    lnkViewAllActivity.Visible = false;
                }
                else
                {
                    literal1.Visible = false;
                }

                if (rptMyPeepsUpdates.Items.Count < DisplayedRows)
                {
                    lnkViewAllActivity.Visible = false;
                }
            }
            catch
            {
                lnkViewAllActivity.Visible = false;
                //throw;
            }
            if (Request.Url.ToString().IndexOf("AllActivity") > -1)
                lnkViewAllActivity.Visible = false;
        }

        private void DisplayError(Exception ex)
        {
            this.Controls.Add(new LiteralControl("<div style='color:Red; display:none;'>" + ex.ToString() + "</div>"));
        }

    }
}